package com.netty.cn.model.MoreClient.client;

import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 
 * @author wangzonghui
 * @date 2021-07-20 10:09:00
 * @Description 客户端chandler
 */
public class MoreClientHandler extends ChannelInboundHandlerAdapter {
	
	private static Logger log = LoggerFactory.getLogger(MoreClientHandler.class);


    public MoreClientHandler() {
    }

    /**
     * 建立连接
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
    	InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        int port = ipSocket.getPort();
        String host = ipSocket.getHostString();
        log.info("与设备"+host+":"+port+"连接成功!");
        ctx.fireChannelActive();
    }

    /**
     * 关闭连接
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
 
        InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        int port = ipSocket.getPort();
        String host = ipSocket.getHostString();
        log.error("与设备"+host+":"+port+"连接断开!");
         
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        System.out.println("服务端返回消息 : [" + msg + "]");
    }

}
